import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/extension/ex_widget.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/ui/common_component.dart';
import 'package:flutter_massage/generated/assets.dart';
import 'package:flutter_massage/pages/model/bean.dart';
import 'package:flutter_massage/pages/my/address/address_bean.dart';
import 'package:flutter_massage/pages/my/order/order_detail/order_detail_logic.dart';
import 'package:flutter_massage/pages/technician/place_order/order_bean.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class OrderDetailPage extends GetView<OrderDetailLogic> {
  const OrderDetailPage(this.bean, {super.key});

  final OrderBean bean;

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: '订单详情',
      scaffoldBackgroundColor: AppColors.grayF8,
      child: GetBuilder<OrderDetailLogic>(
        init: OrderDetailLogic(),
        assignId: true,
        builder: (logic) {
          return SingleChildScrollView(
            padding: EdgeInsets.all(12.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                if (bean.addressBean != null) OrderDetailAddress(bean.addressBean!),
                if (bean.projectBean != null) OrderDetailInfo(bean.projectBean!).paddingTop(12.w),
                OrderDetailMoney(bean).paddingTop(12.w),
              ],
            ),
          );
        },
      ),
    );
  }
}

class OrderDetailAddress extends StatelessWidget {
  const OrderDetailAddress(this.bean, {super.key});

  final AddressBean bean;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Assets.svgHomeLocation.toSvg(width: 13.w),
            CommonMargin.width(3.w),
            TextWidget.oneLine(
              text: bean.name ?? '',
              fontSize: 12,
              color: AppColors.textLevelOne,
              fontWeight: FontWeight.bold,
            ).flexible(),
            CommonMargin.width(10.w),
            TextWidget.oneLine(
              text: bean.phone.encryptPhone,
              fontSize: 12,
              color: AppColors.textLevelOne,
              fontWeight: FontWeight.bold,
            ),
          ],
        ),
        TextWidget.oneLine(
          text: '地址：${bean.address}',
          fontSize: 12,
          color: AppColors.textLevelThree,
        ).paddingTop(8.w),
      ],
    ).card(padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.w), radius: 11.w);
  }
}

class OrderDetailInfo extends StatelessWidget {
  const OrderDetailInfo(this.bean, {super.key});

  final ProjectBean bean;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: CommonDecoration.getRadius(all: 8.w, bg: AppColors.white),
      child: CommonListItemScaffold(
        contentPadding: EdgeInsets.all(16.w),
        left: ImageWidget.url(bean.itemImage ?? '', width: 80.w, height: 80.w, radius: 6.w),
        center: CommonMargin.width(8.w),
        right: SizedBox(
          height: 80.w,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextWidget.moreLine(
                text: bean.itemName ?? '',
                fontSize: 15,
                color: AppColors.textLevelOne,
                fontWeight: FontWeight.bold,
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
              const TextWidget.oneLine(
                text: 'x 1',
                fontSize: 11,
                color: AppColors.textLevelOne,
              ).paddingTop(6.w),
              const Spacer(),
              Text.rich(
                TextSpan(
                  text: '¥',
                  children: [
                    TextSpan(
                      text: bean.itemPrice.removeYuan(),
                      style: const TextStyle(
                        fontSize: 17,
                        color: AppColors.primaryOrange,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                  style: const TextStyle(
                    fontSize: 12,
                    color: AppColors.primaryOrange,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ],
          ),
        ).expanded(),
      ),
    );
  }
}

class OrderDetailMoney extends StatelessWidget {
  const OrderDetailMoney(this.bean, {super.key});

  final OrderBean bean;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildItem('服务技师', bean.technicianBean?.name ?? ''),
        _buildItem('服务时间', bean.time ?? ''),
        _buildItem('备注', bean.desc ?? ''),
        const CommonDivider().paddingTop(10.w),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            const TextWidget.oneLine(
              text: '订单金额：',
              fontSize: 12,
              color: AppColors.textLevelOne,
            ),
            Text.rich(
              TextSpan(
                text: '¥',
                children: [
                  TextSpan(
                    text: bean.projectBean?.itemPrice.removeYuan(),
                    style: const TextStyle(
                      fontSize: 17,
                      color: AppColors.primaryOrange,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
                style: const TextStyle(
                  fontSize: 12,
                  color: AppColors.primaryOrange,
                  fontWeight: FontWeight.bold,
                ),
              ),
            )
          ],
        ).paddingTop(10.w),
      ],
    ).card(radius: 8.w, padding: EdgeInsets.fromLTRB(12.w, 10.w, 12.w, 10.w));
  }

  Widget _buildItem(String title, String content) {
    return Row(
      children: [
        TextWidget.oneLine(
          text: title,
          fontSize: 12,
          color: AppColors.textLevelOne,
        ).paddingRight(5.w),
        TextWidget.oneLine(
          text: content,
          fontSize: 12,
          color: AppColors.textLevelOne,
          fontWeight: FontWeight.bold,
        ).alignRight().expanded(),
      ],
    ).paddingVertical(6.w);
  }
}
